// const path = require('path');
// const Highcharts = require(path.join(__dirname, '../../', '/node_modules/highcharts'));
// var router = require('koa-router')();
// var http = require('http');
// This is how a module is loaded. Pass in Highcharts as a parameter.
// require(path.join(__dirname, '../../', '/node_modules/highcharts/modules/exporting'))(Highcharts);

var sum_now = {},
    zj = 0,
    sh = 0,
    js = 0;
var deepCopy = function (o) {
    if (o instanceof Array) {
        var n = [];
        for (var i = 0; i < o.length; ++i) {
            n[i] = deepCopy(o[i]);
        }
        return n;

    } else if (o instanceof Object) {
        var n = {}
        for (var i in o) {
            n[i] = deepCopy(o[i]);
        }
        return n;
    } else {
        return o;
    }
}
var start = function (chunk) {
    zj = chunk.constant[0];
    sh = chunk.constant[1];
    js = chunk.constant[2];
    $('#zj .value').html(zj);
    $('#sh .value').html(sh);
    $('#js .value').html(js);
    var newDate = new Date();
    newDate = newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate();
    if (Object.keys(sum_now).length == 0 || newDate != sum.dates[sum.dates.length - 1]) {
        sum_now = deepCopy(chunk);
        sum_now.dates.pop();
        sum_now.values[0].data.pop();
        sum_now.values[1].data.pop();
        sum_now.values[2].data.pop();
        if(sum_now.dates.length>60) {
            var dc = sum_now.dates.length - 60;
            sum_now.dates.splice(0, sum_now.dates.length - 60);
            sum_now.values[0].data.splice(0, dc);
            sum_now.values[1].data.splice(0, dc);
            sum_now.values[2].data.splice(0, dc);
        }
        sum_now.values[0].color = '#ff7b8b';
        sum_now.values[1].color = '#f6bf5b';
        sum_now.values[2].color = '#6cc5ff';
        Highcharts.chart('chart', {
            chart: {
                type: 'areaspline',
                backgroundColor: 'rgba(255,255,255,0)',
                plotBackgroundColor: 'rgba(255,255,255,.1)',
                zoomType: 'x'
            },
            title: {
                text: null,
            },
            legend: {
                enabled: false
            },
            xAxis: {
                categories: sum_now.dates,
                tickmarkPlacement: 'on',
                tickLength: 5,
                tickWidth: 5,
                tickColor: "#7ba1ce",
                labels: {
                    style: {
                        "fontSize": '16px',
                        "fontFamily": 'Microsoft YaHei',
                        "color": '#fff'
                    }
                }
            },
            yAxis: {
                title: {
                    text: '(万条)',
                    x: -20,
                    y: -165,
                    rotation: 0,
                    style: {
                        color: '#fff'
                    }
                },
                gridLineWidth: 0,
                lineWidth: 1,
                labels: {
                    enabled: true,
                    formatter: function () {
                        return parseInt(this.value / 10000)
                    },
                    style: {
                        "fontSize": '16px',
                        "fontFamily": 'Microsoft YaHei',
                        "color": "#fff"
                    }
                },
            },
            tooltip: {
                valueSuffix: ' 万条',
                enabled: false,
                formatter: function () {
                    return parseInt(this.y / 10000) + ' 万条';
                }
            },
            credits: {
                enabled: false
            },
            plotOptions: {
                areaspline: {
                    fillOpacity: 0.5
                },
                series: {
                    dataLabels: {
                        enabled: true,
                        color: '#fff',
                        formatter: function () {
                            return parseInt(this.y / 10000) + ' 万条'
                        }
                    }
                }
            },
            series: sum_now.values
        });
    }
}
!function home() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var dates = date.getDate();
    document.getElementsByClassName('year')[0].innerHTML = year + "年";
    document.getElementsByClassName('time')[0].innerHTML = month + "月" + dates + "日";
    var fileUrl = '/json/csj';
    setInterval(function () {
        $.ajaxSetup({
            cache: false
        })
        $.get(fileUrl, function (data) {
            sum = typeof data == 'string' ? JSON.parse(data) : data;
            start(sum)
        })
    }, 5000);
    
    $.get(fileUrl, function (data) {
        sum = typeof data == 'string' ? JSON.parse(data) : data;
        start(sum)
    })
}()

//module.exports = home;
